<template>
	<div class="home-container">
		<el-row :gutter="15">
			<el-col :sm="6" class="mb15">
				<div class="home-card-item home-card-first">
					<div class="flex-margin flex">
						<img v-if="userInfo.avatar" :src="userInfo.avatar" />
						<img v-else :src="avatar" />
						<div class="home-card-first-right ml15">
							<div class="flex-margin">
								<div class="home-card-first-right-title">
									{{ currentTime }}，{{ userInfo.userName === '' ? '游客' : userInfo.userName }}！
								</div>
								<div class="home-card-first-right-msg mt5">{{ userInfo.loginName }}</div>
							</div>
						</div>
					</div>
				</div>
			</el-col>
			<el-col :sm="6" class="mb15" v-for="(v, k) in topCardItemList" :key="k">
				<div class="home-card-item home-card-item-box" :style="{ background: v.color }">
					<div class="home-card-item-flex">
						<div class="home-card-item-title pb3">{{ v.title }}</div>
						<div class="home-card-item-title-num pb6" :id="`titleNum${k + 1}`"></div>
						<div class="home-card-item-tip pb3">{{ v.tip }}</div>
						<div class="home-card-item-tip-num" :id="`tipNum${k + 1}`"></div>
					</div>
					<i :class="v.icon" :style="{ color: v.iconColor }"></i>
				</div>
			</el-col>
		</el-row>
		<el-row :gutter="15" v-if="thirdApps.length > 0">
			<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb15">
				<el-card shadow="hover" header="我的应用">
					<div class="home-item-third-app" v-for="app in thirdApps" :key="app.id">
						<router-link class="color-info" style="text-decoration: none;" target="_blank"
							:to="{path: '/oauth/authorize', query: {appId: app.id, token: Session.get('token')}}">
							<el-image class="home-item-third-app-img" v-if="app.appLogo" :src="app.appLogo" fit="contain">
								<template #error>
									<el-image class="home-item-third-app-img" src="/favicon.png" fit="contain"></el-image>
								</template>
							</el-image>
							<el-image class="home-item-third-app-img" v-else src="/favicon.png" fit="contain"></el-image>
							<div class="home-item-third-app-text">
								{{app.appName}}
							</div>
						</router-link>
					</div>
				</el-card>
			</el-col>
		</el-row>
		<el-row :gutter="15">
			<el-col :xs="24" :sm="14" :md="14" :lg="16" :xl="16" class="mb15">
				<el-card shadow="hover" header="用户月登录详情">
					<div style="height: 200px" ref="userLoginInfoRef"></div>
				</el-card>
			</el-col>
			<el-col :xs="24" :sm="10" :md="10" :lg="8" :xl="8" class="mb15">
				<el-card shadow="hover" header="浏览器使用分布详情">
					<div style="height: 200px" ref="userLoginInfoBrowserRef"></div>
				</el-card>
			</el-col>
		</el-row>
		<el-row :gutter="15">
			<el-col :xs="24" :sm="14" :md="14" :lg="16" :xl="16" class="mb15">
				<el-card shadow="hover" header="用户月操作详情">
					<div style="height: 200px" ref="userOperLogRef"></div>
				</el-card>
			</el-col>
			<el-col :xs="24" :sm="10" :md="10" :lg="8" :xl="8">
				<el-card shadow="hover" header="用户操作分布详情">
					<div style="height: 200px" ref="userOperLogBusinessLabelRef"></div>
				</el-card>
			</el-col>
		</el-row>
		
		<EditPersonal ref="editPersonalRef" @submit="onRefreshCurrentTagsView"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted, nextTick, computed, getCurrentInstance, watch, onActivated } from 'vue';
import * as echarts from 'echarts';
import { useRoute } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
import { CountUp } from 'countup.js';
import { formatAxis, getWeek } from '/@/utils/formatTime';
import { Session } from '/@/utils/storage';
import EditPersonal from '/@/views/personal/editPersonal.vue';
import { topCardItemList, loginInfoOption, operLogOption, browserOption, businessLabelOption} from './mock';
import { countLoignInfo, countOperLog, countLoignInfoPerMonth, countOperLogPerMonth, browserLoignInfo, businessLabelOperLog, getUserThirdAppList } from '/@/api/home/index';

export default {
	name: 'home',
	components: { EditPersonal },
	setup() {
		const { proxy } = getCurrentInstance() as any;
		const userInfo = Session.get('userInfo');
		const storesTagsViewRoutes = useTagsViewRoutes();
		const { isTagsViewCurrenFull } = storeToRefs(storesTagsViewRoutes);
		const userLoginInfoRef = ref();
		const userLoginInfoBrowserRef = ref();
		const userOperLogRef = ref();
		const userOperLogBusinessLabelRef = ref();
		const editPersonalRef = ref();
		const route = useRoute();
		const state = reactive({
			topCardItemList,
			myCharts: [],
			avatar: '/images/profile.png',
			thirdApps: [],
		});
		// 检查当前用户信息
		const checkUserInfo = () => {
			if(userInfo.changeReason != '0'){
				let row = {
					id: userInfo.id,
					loginName: userInfo.loginName,
					userName: userInfo.userName,
					phone: userInfo.phone,
					email: userInfo.email,
					editType: 'pwd',
					changeReason: userInfo.changeReason,
				}
				editPersonalRef.value.openDialog(row);
			}
		}
		const onRefreshCurrentTagsView = () => {
			proxy.mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 0, ...route }));
		};
		// 当前时间提示语
		const currentTime = computed(() => {
			return formatAxis(new Date());
		});
		// 初始化数字滚动
		const initNumCountUp = () => {
			nextTick(() => {
				countLoignInfo().then(res => {
					if(res.code == 0){
						new CountUp('titleNum1', res.data).start();
					}
				});
				countLoignInfo({status: 1}).then(res => {
					if(res.code == 0){
						new CountUp('tipNum1', res.data).start();
					}
				});
				countOperLog().then(res => {
					if(res.code == 0){
						new CountUp('titleNum2', res.data).start();
					}
				});
				countOperLog({status: 1}).then(res => {
					if(res.code == 0){
						new CountUp('tipNum2', res.data).start();
					}
				});
				new CountUp('titleNum3', Math.floor((new Date().getMonth() + 3) / 3)).start();
				new CountUp('tipNum3', getWeek(new Date())).start();
			});
		};
		// 用户月登录详情
		const initUserLoginInfo = async () => {
			const option = loginInfoOption;
			await countLoignInfoPerMonth().then(res => {
				if(res.code == 0){
					option.series[0].data = res.data;
				}
			})
			await countLoignInfoPerMonth({status: 1}).then(res => {
				if(res.code == 0){
					option.series[1].data = res.data;
				}
			})
			const myChart = echarts.init(userLoginInfoRef.value);
			myChart.setOption(option);
			state.myCharts.push(myChart);
		};
		// 用户月登录浏览器详情
		const initUserLoginInfoBrowser = async () => {
			const option = browserOption;
			await browserLoignInfo().then(res => {
				if(res.code == 0){
					option.series[0].data = res.data;
				}
			})
			const myChart = echarts.init(userLoginInfoBrowserRef.value);
			myChart.setOption(option);
			state.myCharts.push(myChart);
		};
		// 用户月操作详情
		const initUserOperLog = async () => {
			const option = operLogOption;
			await countOperLogPerMonth().then(res => {
				if(res.code == 0){
					option.series[0].data = res.data;
				}
			})
			await countOperLogPerMonth({status: 1}).then(res => {
				if(res.code == 0){
					option.series[1].data = res.data;
				}
			})
			const myChart = echarts.init(userOperLogRef.value);
			myChart.setOption(option);
			state.myCharts.push(myChart);
		};
		// 用户操作类型分布详情
		const initUserOperLogBusinessLabel = async () => {
			const option = businessLabelOption;
			await businessLabelOperLog().then(res => {
				if(res.code == 0){
					option.series[0].data = res.data;
				}
			})
			const myChart = echarts.init(userOperLogBusinessLabelRef.value);
			myChart.setOption(option);
			state.myCharts.push(myChart);
		};
		//获取登录用户的第三方应用列表
		const initUserThirdAppList = () => {
			getUserThirdAppList({}).then(res => {
				if(res.code === 0){
					state.thirdApps = res.data;
				}
			})
		}
		// 批量设置 echarts resize
		const initEchartsResizeFun = () => {
			nextTick(() => {
				for (let i = 0; i < state.myCharts.length; i++) {
					state.myCharts[i].resize();
				}
			});
		};
		// 批量设置 echarts resize
		const initEchartsResize = () => {
			window.addEventListener('resize', initEchartsResizeFun);
		};
		// 由于页面缓存原因，keep-alive
		onActivated(() => {
			initEchartsResizeFun();
		});
		// 监听 vuex 中的 tagsview 开启全屏变化，重新 resize 图表，防止不出现/大小不变等
		watch(
			() => isTagsViewCurrenFull.value,
			() => {
				initEchartsResizeFun();
			}
		);
		// 页面加载时
		onMounted(() => {
			checkUserInfo();
			initNumCountUp();
			initUserLoginInfo();
			initUserLoginInfoBrowser();
			initUserOperLog();
			initUserOperLogBusinessLabel();
			initUserThirdAppList();
			initEchartsResize();
		});
		return {
			editPersonalRef,
			userLoginInfoRef,
			userLoginInfoBrowserRef,
			userOperLogRef,
			userOperLogBusinessLabelRef,
			userInfo,
			currentTime,
			onRefreshCurrentTagsView,
			Session,
			...toRefs(state),
		};
	},
};
</script>

<style scoped lang="scss">
.home-container {
	overflow-x: hidden;
	.home-card-item {
		width: 100%;
		height: 103px;
		background: var(--el-text-color-secondary);
		border-radius: 4px;
		transition: all ease 0.3s;
		&:hover {
			box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
			transition: all ease 0.3s;
		}
	}
	.home-card-item-box {
		display: flex;
		align-items: center;
		position: relative;
		overflow: hidden;
		&:hover {
			i {
				right: 0px !important;
				bottom: 0px !important;
				transition: all ease 0.3s;
			}
		}
		i {
			position: absolute;
			right: -10px;
			bottom: -10px;
			font-size: 70px;
			transform: rotate(-30deg);
			transition: all ease 0.3s;
		}
		.home-card-item-flex {
			padding: 0 20px;
			color: var(--color-whites);
			.home-card-item-title,
			.home-card-item-tip {
				font-size: 13px;
				color: var(--el-color-white);
			}
			.home-card-item-title-num {
				font-size: 18px;
				color: var(--el-color-white);
			}
			.home-card-item-tip-num {
				font-size: 13px;
				color: var(--el-color-white);
			}
		}
	}
	.home-item-third-app {
		display: inline-block;
		width: 135px;
		padding: 5px;
		cursor: pointer;
		text-align: center;
		margin-right: 10px;
		&-img {
			height: 65px;
		}
		&-text {
			line-height: 30px;
			overflow: hidden;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 1;
		}
	}
	.home-card-first {
		background: var(--el-color-white);
		border: 1px solid var(--el-border-color-light, #ebeef5);
		display: flex;
		align-items: center;
		img {
			width: 60px;
			height: 60px;
			border-radius: 100%;
			border: 2px solid var(--color-primary-light-5);
		}
		.home-card-first-right {
			flex: 1;
			display: flex;
			flex-direction: column;
			.home-card-first-right-title {
				color: var(--el-color-black);
			}
			.home-card-first-right-msg {
				font-size: 13px;
				color: var(--el-text-color-secondary);
			}
		}
	}
	.home-monitor {
		height: 200px;
		.flex-warp-item {
			width: 50%;
			height: 100px;
			display: flex;
			.flex-warp-item-box {
				margin: auto;
				height: auto;
				text-align: center;
				color: var(--el-text-color-primary);
			}
		}
	}
	.home-warning-card {
		height: 292px;
		:deep(.el-card) {
			height: 100%;
		}
	}
	.home-dynamic {
		height: 200px;
		.home-dynamic-item {
			display: flex;
			width: 100%;
			height: 60px;
			overflow: hidden;
			&:first-of-type {
				.home-dynamic-item-line {
					i {
						color: orange !important;
					}
				}
			}
			.home-dynamic-item-left {
				text-align: right;
				.home-dynamic-item-left-time1 {
				}
				.home-dynamic-item-left-time2 {
					font-size: 13px;
					color: var(--el-text-color-secondary);
				}
			}
			.home-dynamic-item-line {
				height: 60px;
				border-right: 2px dashed var(--el-border-color-light, #ebeef5);
				margin: 0 20px;
				position: relative;
				i {
					color: var(--color-primary);
					font-size: 12px;
					position: absolute;
					top: 1px;
					left: -6px;
					transform: rotate(46deg);
					background: var(--el-color-white);
				}
			}
			.home-dynamic-item-right {
				flex: 1;
				.home-dynamic-item-right-title {
					i {
						margin-right: 5px;
						border: 1px solid var(--el-border-color-light, #ebeef5);
						width: 20px;
						height: 20px;
						border-radius: 100%;
						padding: 3px 2px 2px;
						text-align: center;
						color: var(--color-primary);
					}
				}
				.home-dynamic-item-right-label {
					font-size: 13px;
					color: var(--el-text-color-secondary);
				}
			}
		}
	}
}
</style>
